/*
 * @lc app=leetcode.cn id=143 lang=cpp
 *
 * [143] 重排链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
  void reorderList(ListNode* head)
  {
    vector<ListNode*> nodes;
    for (; head; head = head->next)
      nodes.push_back(head);
    ListNode* tmp = nodes[0];
    int cnt = 0;
    int L = 0, R = nodes.size();
    while (L < R) {
      if (++cnt % 2)
        tmp->next = nodes[--R];
      else
        tmp->next = nodes[++L];
      tmp = tmp->next;
    }
    tmp->next = nullptr;
  }
};
// @lc code=end
